<template>
  <div class="login">
    <div class="left">
      <div class="title">
        <img class="t1" src="@/assets/img/login_logo.png" alt="" />
        <span class="t2">黑马面面</span>
        <div class="t3"></div>
        <span class="t4">用户登录</span>
      </div>
      <el-form :model="form" class="form" :rules="rules" ref="form">
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input
                v-model="form.code"
                placeholder="请输入验证码"
                prefix-icon="el-icon-key"
              ></el-input>
            </el-col>
            <el-col :span="8">
              <img
                class="login-code"
                @click="imgClick"
                :src="baseUrl + gcodeUrl"
                alt=""
              />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="isPass">
          <el-checkbox v-model="form.isPass" class="isPass" :label="true">
            <span>我已阅读并同意</span>
            <el-link href="#" type="primary" class="protocol">用户协议</el-link>
            <span>和</span>
            <el-link href="#" type="primary" class="protocol">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="btn" @click="toLogin"
            >登录</el-button
          >
          <br />
          <el-button type="primary" class="btn" @click="submit">注册</el-button>
        </el-form-item>
      </el-form>
    </div>
    <img class="rightImg" src="@/assets/img/login_img.png" alt="" />

    <!-- 注册组件 -->

    <register ref="register"></register>
  </div>
</template>
<script>
import register from './register'
import {userLogin} from '@/api/login.js'
import {saveLocal,getlocal} from '@/utils/local.js'
export default {
  components: {
    register
  },
  data () {
    return {
      baseUrl: process.env.VUE_APP_URL,
      gcodeUrl: '/captcha?type=login',
      form: {
        phone: '',
        password: '',
        code: '',
        isPass: []
      },
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change' },
          {
            validator: (rule, value, callback) => {
              let reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (reg.test(value)) {
                callback()
              } else {
                callback(new Error('请正确输入手机号'))
              }
            }
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 12, message: '请输入6-12位密码', trigger: 'change' }
        ],
        code: [
          { required: true, message: '请输入验证码', trigger: 'change' },
          { min: 4, max: 4, message: '请输入4位验证码', trigger: 'change' }
        ],
        isPass: [{ required: true, message: '请勾选协议', trigger: 'change' }]
      }
    }
  },
  methods: {
    submit () {
      this.$refs.register.isShow = true
    },
    toLogin () {
      this.$refs.form.validate(result => {
        if (result) {
          userLogin(this.form).then(res=>{
            console.log(res); 
           this.$message.success('登录成功')
            // window.localStorage.setItem('token',res.data.data.token)
            saveLocal('token',res.data.data.token)
            this.$router.push('/layout');
          }).catch(error=>{
            console.log(error);
            // this.$message.error(err)
          })
        } else {
          this.$message.error('验证失败')
        }
      })
    },
    imgClick () {
      this.gcodeUrl = '/captcha?type=login&sdeef=' + Date.now()
    }
  },
  created () {
    // alert(process.env.VUE_APP_5URL)
    if(getlocal('token')){
      this.$router.push('/layout')
    }
  }
}
</script>
<style lang="less">
.login {
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  justify-content: space-around;
  align-items: center;

  .left {
    width: 478px;
    height: 550px;
    background: #f5f5f5;
    padding: 50px;

    .title {
      display: flex;
      align-items: center;

      .t1 {
        width: 22px;
        height: 18px;
      }
      .t2 {
        font-size: 24px;
        font-family: SourceHanSansCN Regular, SourceHanSansCN Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
        margin: 0 16px;
      }
      .t3 {
        width: 1px;
        height: 28px;
        background: #c7c7c7;
        margin-right: 12px;
      }
      .t4 {
        font-size: 22px;
        font-family: PingFangSC Regular, PingFangSC Regular-Regular;
        font-weight: 400;
        text-align: left;
        color: #0c0c0c;
      }
    }
    .form {
      margin-top: 30px;
      .login-code {
        width: 100%;
        height: 40px;
      }
      .btn {
        display: block;
        width: 100%;
      }
      .btn:nth-child(2) {
        margin-top: 20px;
      }
      .isPass {
        display: flex;
        align-items: center;
      }
      .protocol {
        vertical-align: bottom;
      }
    }
  }
}
</style>
